package com.hdcx.Minfresh.adapter;


import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.BaseActivity;
import com.hdcx.Minfresh.bean.DiscountWrapper;

/**
 * Created by Administrator on 2016/11/9.
 */

public class DiscountAdapter extends RecyclerView.Adapter<DiscountAdapter.DisHolder> {
    BaseActivity mActivity;
    private int select_pos = -1;
    DiscountWrapper data;

    public DiscountAdapter(BaseActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public DisHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        view = layoutInflater.inflate(R.layout.item_discount_list_, parent, false);
        return new DisHolder(view);
    }

    @Override
    public void onBindViewHolder(DisHolder holder, final int position) {
        final DiscountWrapper.LIST temp = data.getList().get(position);
        holder.tv_title.setText(temp.getBonus_name());
        holder.tv_money.setText(temp.getBonus_value());
        holder.tv_start_time.setText(temp.getStart_time());
        holder.tv_end_time.setText(temp.getEnd_time());
        holder.img_select.setImageResource(select_pos==position?R.drawable.ic_gou:R.drawable.ic_quan);
        holder.layout_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select_pos =  select_pos==position?-1:position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        }
        return data.getList().size();
    }

    public void setData(DiscountWrapper response) {
        data = response;
        for(int i=0;i<data.getList().size();i++){
            data.getList().get(i).setIs_select(false);
        }
        notifyDataSetChanged();
    }

    public void setSureOnclik(TextView sure) {
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if(select_pos==-1){
                    intent.putExtra("Disname","");
                    intent.putExtra("id","");
                }else{
                    intent.putExtra("Disname",data.getList().get(select_pos).getBonus_value());
                    intent.putExtra("id",data.getList().get(select_pos).getId());
                }
                mActivity.setResult(Activity.RESULT_OK,intent);
                mActivity.finish();
            }
        });
    }

    public class DisHolder extends RecyclerView.ViewHolder {
        public TextView tv_money, tv_start_time, tv_title, tv_end_time;
        LinearLayout layout_all;
        public ImageView img_select;

        public DisHolder(View itemView) {
            super(itemView);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_start_time = (TextView) itemView.findViewById(R.id.tv_start_time);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_end_time = (TextView) itemView.findViewById(R.id.tv_end_time);
            layout_all = (LinearLayout) itemView.findViewById(R.id.layout_all);
            img_select = (ImageView) itemView.findViewById(R.id.img_select);
        }
    }
}
